package com.atguigu.day05;

import com.atguigu.bean.Event;
import com.atguigu.day03.Flink05_Source_Custom;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.SlidingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.util.HashSet;

public class Flink07_ProcessTimeWindow_AggFunWithProcessWindowFun {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2.通过自定义数据源获取数据
        DataStreamSource<Event> streamSource = env.addSource(new Flink05_Source_Custom.ClickSource());

        //3.将相同url的数据聚合达到同一个分区
        KeyedStream<Event, String> keyedStream = streamSource.keyBy(new KeySelector<Event, String>() {
            @Override
            public String getKey(Event value) throws Exception {
                return value.url;
            }
        });

        //4.开启一个滑动窗口，每5秒钟计算一次，计算10秒的数据
        WindowedStream<Event, String, TimeWindow> window = keyedStream.window(SlidingProcessingTimeWindows.of(Time.seconds(10), Time.seconds(5)));

        // 5.先使用增量聚合函数得到这个页面的点击量，然后再利用全窗口函数，打上窗口的起始和结束时间
        window.aggregate(new AggregateFunction<Event, Integer, Integer>() {
            @Override
            public Integer createAccumulator() {
                return 0;
            }

            @Override
            public Integer add(Event value, Integer accumulator) {
                return accumulator + 1;
            }

            @Override
            public Integer getResult(Integer accumulator) {
                return accumulator;
            }

            @Override
            public Integer merge(Integer a, Integer b) {
                return a + b;
            }
        },
                //全窗口函数中获取的数据其实就是上面增量函数计算后的结果，一个窗口一般输出一个结果，那么全窗口函数就只接受到一个数据
                new ProcessWindowFunction<Integer, String, String, TimeWindow>() {
            @Override
            public void process(String s, ProcessWindowFunction<Integer, String, String, TimeWindow>.Context context, Iterable<Integer> elements, Collector<String> out) throws Exception {
//                Integer sumCount = 0;

                Integer count=0;

                for (Integer element : elements) {
                    count= element;
                }

                out.collect("[" + context.window().getStart() + "," + context.window().getEnd() + ")" + "热门度为：" + count + "数据个数为：" + elements.spliterator().estimateSize());

            }
        }).print();

        env.execute();
    }
}
